package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.NoviceCreditCardList;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;
import java.util.Locale;

/**
 * Created by Jiangzx on 16:30.
 * 新手办卡
 */

public class NoviceCreditCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int NUMBER_MILLION=10000;
    private static final int TYPE_NO_DATA=1;
    private static final int TYPE_CONTENT=2;

    private RecycleItemClickedCallback clickedCallback;
    private List<NoviceCreditCardList.NoviceCreditCard> data;

    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback) {
        this.clickedCallback = clickedCallback;
    }

    int radian;

    public NoviceCreditCardAdapter(List<NoviceCreditCardList.NoviceCreditCard> data) {
        this.data = data;
        this.radian = DensityUtil.dp2px(5.0f);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        if(viewType==TYPE_NO_DATA){
            View itemView=inflater.inflate(R.layout.item_no_data,parent,false);
            return new NoDataHolder(itemView);
        }
        if(viewType==TYPE_CONTENT){
            View itemView=inflater.inflate(R.layout.item_novice_credit_card,parent,false);
            return new NoviceCreditCardHolder(itemView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof NoviceCreditCardHolder) {
            NoviceCreditCardHolder cardHolder = (NoviceCreditCardHolder) holder;
            if(position==0){
                RecyclerView.MarginLayoutParams params=
                        (RecyclerView.MarginLayoutParams) cardHolder.itemView.getLayoutParams();
                params.topMargin=DensityUtil.dp2px(16);
            }
            switch (position) {
                case 0:
                    cardHolder.tvTopPosition.setText("推荐：NO.1");
                    cardHolder.tvTopPosition.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    cardHolder.tvTopPosition.setText("推荐：NO.2");
                    cardHolder.tvTopPosition.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    cardHolder.tvTopPosition.setText("推荐：NO.3");
                    cardHolder.tvTopPosition.setVisibility(View.VISIBLE);
                    break;
                default:
                    cardHolder.tvTopPosition.setVisibility(View.GONE);
            }
            NoviceCreditCardList.NoviceCreditCard card = data.get(position);
            cardHolder.tvName.setText(card.getName());
            cardHolder.tvContentTop.setText(card.getMarketing_one());
            cardHolder.tvContentBottom.setText(card.getMarketing_two());
            int applyCount=card.getApply_count();
            if(applyCount>=NUMBER_MILLION){
                if(applyCount%NUMBER_MILLION==0){
                    cardHolder.tvApplyCount.setText((applyCount/NUMBER_MILLION)+"万");
                }else{
                    //判断是否为避免x.0万
                    String formatStr=String.format(Locale.CHINESE,"%.1f",(float)applyCount/NUMBER_MILLION);
                    if(formatStr.endsWith("0")){
                        cardHolder.tvApplyCount.setText(formatStr.substring(0,formatStr.indexOf('.'))+"万");
                    }else{
                        cardHolder.tvApplyCount.setText(formatStr+"万");
                    }
                }
            }else{
                cardHolder.tvApplyCount.setText(String.valueOf(applyCount));
            }
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL + card.getPicture_path(),
                    cardHolder.ivPic,
                    R.mipmap.img_default_rectangle,
                    radian);
            cardHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickedCallback != null) {
                        clickedCallback.onRecycleItemClicked(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return (data == null||data.size()==0) ? 1 : data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return (data==null||data.size()==0)?TYPE_NO_DATA:TYPE_CONTENT;
    }

    private class NoviceCreditCardHolder extends RecyclerView.ViewHolder {
        private TextView tvTopPosition;
        private TextView tvName;
        private TextView tvContentTop;
        private TextView tvContentBottom;
        private TextView tvApplyCount;
        private ImageView ivPic;

        private NoviceCreditCardHolder(View itemView) {
            super(itemView);
            tvTopPosition = (TextView) itemView.findViewById(R.id.tv_top_position);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvContentTop = (TextView) itemView.findViewById(R.id.tv_content_top);
            tvContentBottom = (TextView) itemView.findViewById(R.id.tv_content_bottom);
            tvApplyCount = (TextView) itemView.findViewById(R.id.tv_apply_count);
            ivPic = (ImageView) itemView.findViewById(R.id.image);
        }
    }

    private class NoDataHolder extends RecyclerView.ViewHolder {
        private NoDataHolder(View itemView) {
            super(itemView);
            itemView.setVisibility(View.VISIBLE);
        }
    }
}
